package day06;

import java.util.LinkedList;
import java.util.Queue;

public class 二叉树的最大深度 {
    public int maxDepth(TreeNode root) {
        int ret = 0;
        if (root == null) {
            return ret;
        }
        // 用队列
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        while (!q.isEmpty()) {
            int size = q.size();
            while (size-- != 0) {
                TreeNode tmp = q.poll();
                // 加入队列
                if (tmp.left != null) {
                    q.offer(tmp.left);
                }
                if (tmp.right != null) {
                    q.offer(tmp.right);
                }
            }
            ret++;
        }
        return ret;
    }
}
